#!/usr/bin/python
# -*- coding:UTF-8 -*-
# filename:start.py

import datetime, hashlib

class Data:
    
    textEnum = [
        'http_code', 
        'num_connects',
        'num_redirects',
        'time_redirect',
        'time_namelookup',
        'time_connect',
        'time_pretransfer',
        'time_starttransfer',
        'time_total',
        'speed_download',
        'current_time'
    ]
    file = ''
    json = ''
    data = []
    
    def __init__(self, file):
        #赢取文件
        self.file = file
        f = open(file, 'r')
        self.data = f.readlines()
        f.close()
    
    def setJson(self, index):
        #拆解数据
        valuesList = [] 
        labelsList = []
        for line in self.data:
            list = line[:-2].split(':')
            valuesList.append(list[index])
            labelsList.append(list[-1])
        #标签设置
        labelsListTmp = []
        for mTime in labelsList:
            mTimeDate = datetime.datetime.fromtimestamp(int(mTime))
            mTimeDateStr = mTimeDate.strftime('%H:%M')
            labelsListTmp.append(mTimeDateStr)
        #values & labels
        values = '[%s]' % ','.join(valuesList)
        labels = '["%s"]' % '","'.join(labelsListTmp)
        #y轴刻度
        valuesListTmp = []
        for v1Tmp in valuesList:
            valuesListTmp.append(float(v1Tmp))
#        valuesMax = int(max(valuesListTmp)) + 1
        valuesMax = max(valuesListTmp)
        if valuesMax < 0.01:
            valuesMax = 0.01
        #Json数据设置
        self.setJsonView(values, labels, line, valuesMax, index)

    def setJsonView(self, values, labels, line, valuesMax, index):
        md5 = hashlib.md5()
        md5.update(values)
        randomColor = md5.hexdigest()[8:14]
        y_legend = '"y_legend":{"text":"Time of day","style":"{color:#736AFF;}"}' 
        line = '{"type":"line","colour":"#%s","text":"%s","font-size":10,"width":2,"dot-size":4,"halo-size":0,"values":%s}' % (randomColor, self.textEnum[index], values) 
        elements = '"elements":[%s]' % line 
        x_axis =  '"x_axis":{"labels":{"rotate":"vertical","labels":%s}}' % labels
        y_axis =  '"y_axis":{"max":%s}' % valuesMax
        self.json = '{%s,%s,%s,%s}' % (y_legend, elements, x_axis, y_axis)

################################################################################

import web, datetime, os

urls = (
    '/getdata/(\d{4}-\d{2}-\d{2})/(\d+)/(\d).*', 'GetData',
    '/(.*)', 'Index'
)

app = web.application(urls, globals())

class Index:
    def GET(self, path):
        outstr = 'Welcome to Index, URI is /{0}, path is {0}\n'.format(path)
        return outstr

class GetData:
    
    
    def GET(self, selDate, id, index):
        if selDate == '0000-00-00':
            selDate = datetime.date.today()
        file = "data/%s/%s" % (selDate, id)
        index = int(index)
        data = Data(file)
        if os.path.isfile(file):
            data.setJson(index)
        return data.json

if __name__ == '__main__':
    app.run()

